import {defineStore} from "pinia";
import Post = API.Post;
import {getPostList,addNewPost,getPostById,searchPost} from "@/api/web/post.js";
import {searchPostByCustomerId} from "@/api/web/post.js";





const usePostStore = defineStore(
    "post",{
        state:()=>({
            postList:[ ] as Post[],
            postListGEN:[] as Post[],
            showPost:{},
            postList2:[] as Post[]
        }),
        getters:{

        },
        actions:{

            addPost(post:API.PostGen){
                return new Promise(async (resolve, reject) => {
                    await addNewPost(post)
                    getPostList().then((res: any) => {
                        this.postList = res.rows
                        this.postListGEN = res.rows
                        console.log(res)
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                    })
                })

            },
            getPostList(){
                return new Promise((resolve, reject) => {
                    console.log("============获取postList()")
                    getPostList().then((res: any) => {
                        this.postList = res.rows
                        this.postListGEN = res.rows
                        console.log(res)
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                    })
                })
            },
            //通过id获取文章信息id
            getPostById(id: number) {
                return new Promise((resolve, reject) => {
                console.log(Date.now()+"::::::::::::::::::::::::")
                console.log("============获取post详细信息" + id)
                 getPostById(id).then((res: any) => {
                    this.showPost = res.data
                    console.log(res)
                     resolve(true)
                 }).catch((error: any) => {
                     reject(error)
                 })
                })
            },
            //通过标题搜索文章
            searchPost2(postTitle:string){
                return new Promise((resolve, reject) => {
                    console.log("============搜索post信息:" + typeof postTitle + "=========")
                        searchPost(postTitle).then((res: any) => {
                            this.postList = res.rows
                            this.postListGEN = res.rows
                            console.log(res)
                            resolve()
                        }).catch((error: any) => {
                            reject(error)
                        })
                })
            },
            //通过发布作者的id获取相关文章
            searchPostByCustomerId(customerId:Number){
                return new Promise((resolve, reject) => {
                    searchPostByCustomerId(customerId).then((res: any) => {
                        this.postList2 = res.rows
                        console.log(res.rows)
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                    })
                })
            }


        }
    })



export default usePostStore
